/**
 * 
 */
package org.apache.log4j;

import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

/**
 * a filter based on regex
 *
 * @author lars
 * @version $Id: $
 */
public class RegexMatchFilter extends Filter
{

	private boolean acceptOnMatch;
	private String regex;

	/**
	 * <p>setRegexToMatch.</p>
	 *
	 * @param regex a {@link java.lang.String} object.
	 */
	public void setRegexToMatch(String regex)
	{
		this.regex = regex;
	}

	/**
	 * <p>Setter for the field <code>acceptOnMatch</code>.</p>
	 *
	 * @param acceptOnMatch a boolean.
	 */
	public void setAcceptOnMatch(boolean acceptOnMatch)
	{
		this.acceptOnMatch = acceptOnMatch;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.log4j.spi.Filter#decide(org.apache.log4j.spi.LoggingEvent)
	 */
	/** {@inheritDoc} */
	@Override
	public int decide(LoggingEvent event)
	{
		String message = event.getMessage().toString();
		int result = Filter.NEUTRAL;

		if (message.matches(regex))
		{
			result = (acceptOnMatch ? Filter.ACCEPT : Filter.DENY);
		}
		else
			if (!message.matches(regex))
			{
				result = (!acceptOnMatch ? Filter.ACCEPT : Filter.DENY);
			}

		return result;
	}
}
